<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class My_Controller extends CI_Controller {
    public $template;
    
    public function __construct() {
        parent::__construct();
        $this->template = 'front/layout';
        
        // Load my config
        $this->config->load('myconfig');
        $this->per_page 	= $this->config->item('per_page_front');
        
        $this->view['title_page'] = "Tra cứu mã số bài hát Karaoke - Địa điểm Karaoke";
        $this->view['description_page'] = "Tra cứu mã số Karaoke Vol 51, Vol 50 mới nhất, hỗ trợ phiên bản mobile và tất cả các trình duyệt";
        $this->view['keyword_page'] = 'ma so karaoke, ma so karaoke vol 51, vol 50, vol 49, vol 48, arirang karaoke, ma so, dia diem karaoke, danh sach quan karaoke ho chi minh';
    }
    
    /**
     * Render template
     * @param $view The child view
     * */
    public function _render($view) {
    	$this->load->view($this->template, $view);
    }
    
    
    public function _pagination($url, $total_rows, $per_page, $uri_segment = '', $page_query_string = false) {
    	$config['use_page_numbers'] = TRUE;
    	
    	if ($page_query_string) {
    		$config['page_query_string'] = TRUE;
    		$config['use_page_numbers'] = TRUE;
    		$config['query_string_segment'] = 'page';
    	}
    	
    	$config['first_link'] = 'Trang đầu';
    	$config['last_link'] = 'Trang cuối';
    	$config['num_links'] = $this->config->item('num_links_pagination');
    	$config['base_url'] = base_url(). $url;
    	$config['total_rows'] = $total_rows;
    	$config['per_page'] = $per_page;
    	$config['full_tag_open'] = '<ul class="pagination">';
    	$config['full_tag_close'] = '</ul>';
    	$config['num_tag_open'] = '<li>';
    	$config['num_tag_close'] = '</li>';
    	
    	$config['first_tag_open'] = '<li>';
    	$config['first_tag_close'] = '</li>';
    	
    	$config['prev_link'] = '&laquo;';
    	$config['next_link'] = '&raquo;';
    	
    	$config['last_tag_open'] = '<li>';
    	$config['last_tag_close'] = '</li>';
    	 
    	$config['next_tag_open'] = '<li>';
    	$config['next_tag_close'] = '</li>';
    	
    	$config['prev_tag_open'] = '<li>';
    	$config['prev_tag_close'] = '</li>';
    	
    	$config['cur_tag_open'] = '<li class="current-page"><a>';
    	$config['cur_tag_close'] = '</a></li>';
    	
    	$config['uri_segment'] = $uri_segment ? $uri_segment : $this->config->item('pag_uri_segment');
    	$this->pagination->initialize($config);
    	
    	return $this->pagination->create_links();
    }
    
    public function vn_str_filter ($str) {

       $unicode = array(

           'a'=>'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ',

           'd'=>'đ',

           'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',

           'i'=>'í|ì|ỉ|ĩ|ị',

           'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',

           'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',

           'y'=>'ý|ỳ|ỷ|ỹ|ỵ',

           'A'=>'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',

           'D'=>'Đ',

           'E'=>'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',

           'I'=>'Í|Ì|Ỉ|Ĩ|Ị',

           'O'=>'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',

           'U'=>'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',

           'Y'=>'Ý|Ỳ|Ỷ|Ỹ|Ỵ',

       );

      foreach($unicode as $nonUnicode=>$uni){

           $str = preg_replace("/($uni)/i", $nonUnicode, $str);

      }

       return $str;

   }
   
    public function get_lnglat($address) {
        echo $address .' <br/>';
        
        $prepAddr = str_replace(' ','+',$address);
        $geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
        $output= json_decode($geocode);
        $latitude = $output->results[0]->geometry->location->lat;
        $longitude = $output->results[0]->geometry->location->lng;
        return array (
            'latitude' => $latitude,
            'longitude' => $longitude,
        );
    }
    
    public function count_total_page($total) {
		return ceil($total/$this->per_page);
	}
}